﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using WindowsApplication1.localhost;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private CalculatorClient client;

        public Form1()
        {
            InitializeComponent();
            panel1.Visible = false;
            buttonAdminTool.Visible = false;
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            textBoxCurrentUsers.Visible = false;
            try
            {
                client = new CalculatorClient();
                client.ClientCredentials.UserName.UserName = textBoxUserName.Text;
                client.ClientCredentials.UserName.Password = maskedTextBoxPassword.Text;
                //client.Divide(5.0D, 2.0D);
                panel1.Visible = true;
                BindingList<string> roles = client.GetUserRoles(textBoxUserName.Text);

                string msg = "You are Logged in and your Roles are :\n";
                foreach (string s in roles)
                {
                    msg += "\t" + s + "\n";
                }

                if (roles.Contains("Super Users"))
                    buttonAdminTool.Visible = true;
                else
                    buttonAdminTool.Visible = false;

                MessageBox.Show(msg);
            }
            catch (Exception ex)
            {
                panel1.Visible = false;
                MessageBox.Show("Exception: " + ex.Message);

                if (ex.InnerException != null)
                    MessageBox.Show("Inner Exception: " + ex.InnerException.Message);
            }
            finally
            {
                if(client!= null && client.State == CommunicationState.Opened)
                client.Close();
            }
        }

        private void buttonExecuteCmd_Click(object sender, EventArgs e)
        {         
            try
            {
                client = new CalculatorClient();
                client.ClientCredentials.UserName.UserName = textBoxUserName.Text;
                client.ClientCredentials.UserName.Password = maskedTextBoxPassword.Text;
                textBoxDBResult.Text = client.DataBaseSelect(textBoxCmd.Text);
                client.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);

                if (ex.InnerException != null)
                    MessageBox.Show("Inner Exception: " + ex.InnerException.Message);
            }
            finally
            {
                if (client.State == CommunicationState.Opened)
                    client.Close();
            }
        }

        private void buttonAdminTool_Click(object sender, EventArgs e)
        {
            AdminForm adminForm = new AdminForm(textBoxUserName.Text, maskedTextBoxPassword.Text);
            adminForm.Show(this);
        }

    }
}